package whf.framework.util.var;

import java.util.Map;

import whf.framework.security.UserContext;
import whf.framework.util.BeanUtils;
import whf.framework.util.ThreadContext;

/**
 * 从用户上下文中解析内容
 * @author wanghaifeng
 * @create Jan 24, 2007 11:42:11 AM
 * 
 */
public class UserContextVariableHolder implements VariableHolder {
	private UserContext userContext;
	
	private UserContextVariableHolder(){
		this.userContext = ThreadContext.getUserContext();
	}
	
	public UserContextVariableHolder(UserContext uc){
		this.userContext = uc;
	}

	/* (non-Javadoc)
	 * @see whf.framework.util.var.VariableHolder#getAny()
	 */
	public Object getAny() {
		return this.userContext.getUser();
	}

	/* (non-Javadoc)
	 * @see whf.framework.util.var.VariableHolder#getHolder()
	 */
	public Map<String, Object> getHolder() {
		return null;
	}

	/* (non-Javadoc)
	 * @see whf.framework.util.var.VariableHolder#getVariable(java.lang.String)
	 */
	public Object getVariable(String key) {
		Object value = null;
		try{
			value = BeanUtils.getProperty(this.userContext, key);
		}catch(Exception e){
			value = this.userContext.getSessionObject(key);
		}
		return value;
	}

	
	/* (non-Javadoc)
	 * @see whf.framework.util.var.VariableHolder#getScope()
	 */
	public VariableHolderScope getScope() {
		return VariableHolderScope.THREAD;
	}
}
